package com.nl.lib_project.datas

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.nl.lib_project.entity.dbentity.AnswerResult

/** 作者: wangfei 创建时间: 2021/10/11 包名: com.nl.lib_project.datas 类名: Converters 将自定义类型的数据持久化到DB */
class DBConverters {

  @TypeConverter
  fun stringToList(value: String): List<String>? {
    if (value.isNullOrBlank()) {
      return null
    }
    return Gson().fromJson(value, List::class.java) as List<String>?
  }

  @TypeConverter
  fun listToString(value: List<String>?): String? {
    if (value == null) {
      return null
    }
    return Gson().toJson(value)
  }

  @TypeConverter
  fun intToList(value: String): List<Int>? {
    if (value.isNullOrBlank()) {
      return null
    }
    return Gson().fromJson(value, List::class.java) as List<Int>?
  }

  @TypeConverter
  fun listToInt(value: List<Int>?): String? {
    if (value == null) {
      return null
    }
    return Gson().toJson(value)
  }

  @TypeConverter
  fun answerResultArrayListToString(list: List<AnswerResult>?): String? {
    if (list.isNullOrEmpty()) {
      return null
    }
    return Gson().toJson(list)
  }

  @TypeConverter
  fun stringToAnswerResultArrayList(value: String): List<AnswerResult>? {
    if (value.isNullOrBlank()) {
      return null
    }

    return Gson().fromJson(value, List::class.java) as List<AnswerResult>?
  }
}
